package com.ch.forum.service.impl;

import com.ch.forum.bo.ResponseResult;
import com.ch.forum.dao.ArticelMapper;
import com.ch.forum.dao.RewardMapper;
import com.ch.forum.dao.UserMapper;
import com.ch.forum.model.RewardModel;
import com.ch.forum.model.UserModel;
import com.ch.forum.service.ArticleService;
import com.ch.forum.service.RewardService;
import com.ch.forum.service.SystemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RewardServiceImpl implements RewardService {

    @Autowired
    private RewardMapper rewardMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SystemParamService systemParamService;

    @Autowired
    private ArticelMapper articelMapper;

    @Override
    public ResponseResult add(RewardModel model) {

        if(model == null){
            return new ResponseResult(false,  "參數不能為空");
        }
        UserModel userModel = userMapper.findById(model.getUserId());
        if(userModel == null){
            return new ResponseResult(false,"用戶不存在");
        }
        if(userModel.getPoint() == null){
            return new ResponseResult(false, "用户没有积分");
        }
        if(userModel.getPoint().compareTo(model.getPrice()) < 0){
            return new ResponseResult(false,"積分不夠打賞");
        }
        model.setDate(new Date(System.currentTimeMillis()));
        model.setStatus(0);
        Long userId = articelMapper.findUserIdByArtId(model.getArtId());
        UserModel model1 = userMapper.findById(userId);
        if(model1.getpId() == null){
            // 打赏人积分减掉
            userMapper.updatePoint(model.getUserId(), userModel.getPoint() - model.getPrice());
            // 作者虚拟积分和积分增加
            String point = systemParamService.findValueByName("PROMOTION_VIRTUAL_POINT");

            userMapper.updateVirtualPoint(userId, model1.getVirtualPoint() + Double.valueOf(point));
            userMapper.updatePoint(userId, model1.getPoint() + model.getPrice());
        }else {
            // 打赏时候二级推广的设置
            String firstProPoint = systemParamService.findValueByName("FIRST_PROMOTION_POINT");
            String secProPoint = systemParamService.findValueByName("SECOND_PROMOTION_POINT");
            // 虚拟积分数值
            String firstProVirPoint = systemParamService.findValueByName("FIRST_PROMOTION_VIR_POINT");
            String secProVirPoint = systemParamService.findValueByName("SECOND_PROMOTION_VIR_POINT");

            // 打赏人积分减掉
            userMapper.updatePoint(model.getUserId(), userModel.getPoint() - model.getPrice());

            // 被打赏积分累加
            userMapper.updateVirtualPoint(userId, model1.getVirtualPoint() + Double.valueOf(secProVirPoint));
            userMapper.updatePoint(userId, model1.getPoint() + (model.getPrice() * Double.valueOf(secProPoint)));

            // 一级的比例
            UserModel model2 = userMapper.findById(model1.getpId());
            userMapper.updateVirtualPoint(model1.getpId(), model2.getVirtualPoint() + Double.valueOf(firstProVirPoint));
            userMapper.updatePoint(model1.getpId(), model2.getPoint() + (model.getPrice() * Double.valueOf(firstProPoint)));

        }
        rewardMapper.add(model);
        return new ResponseResult(true,"打賞成功");
    }

    @Override
    public void updateStatus(Long id) {
        rewardMapper.update(id);
    }

}
